<?php
/**
 * Created by PhpStorm.
 * User: liang
 * Email:1050187159@qq.com
 * Date: 2018/8/14
 * Time: 20:50
 */
namespace Admin\Model;

use Think\Model;
use Admin\Controller\FileController;

class NewsModel extends Model
{
   protected $trueTableName = 'news';

   public function newsAdd()
   {
       $size = I('post.size');
       $type = I('post.type');
       $title = I('post.title');
       $is_enabled = I('post.is_enabled');
       $sort = I('post.sort', 0);
       $content = I('post.content');
       $image = I('post.image');

       if (empty($title)) {
           $this->error = '标题不能为空';
           return false;
       }
       if (mb_strlen($title) > 30) {
           $this->error = '标题长度不能超过30';
           return false;
       }
       if (empty($size)) {
           $this->error = '图片大小必填';
           return false;
       }
       $size = explode('x',$size);
       if ((!is_integer((int)$size[0])) or (!is_integer((int)$size[1]))) {
           $this->error = '图片大小输入的格式错误';
           return false;
       }
       $Image = new \Think\Image();
       $Image->open(APP_PATH.$image);
       $mobile_image = '/thumb/'.date('Y-m-d', time()).'/';
       mkdir(APP_PATH.$mobile_image, 0777, true);
       $mobile_image = $mobile_image.basename($image);
       //按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg
       $Image->thumb((int)$size[0], (int)$size[1])->save(APP_PATH.$mobile_image);
       if (empty($image)) {
           $this->error = '必须上传图片';
           return false;
       }
       if (is_integer($sort)) {
           $this->error = '排序必须为整数';
           return false;
       }
       if (empty($content)) {
           $this->error = '内容不能为空';
           return false;
       }

       $this->title = $title;
       $this->is_enabled = $is_enabled;
       $this->image = $image;
       $this->mobile_image = $mobile_image;
       $this->sort = $sort;
       $this->content = $content;
       $this->type = $type;
       $this->size = I('post.size');
       $this->pub_time = time();
       $this->add();

       return true;
   }

   public function newsModify()
   {
       //接受参数
       $id = I('post.id');
       $size = I('post.size');
       $type = I('post.type');
       $title = I('post.title');
       $is_enabled = I('post.is_enabled');
       $sort = I('post.sort', 0);
       $content = I('post.content');
       $image = I('post.image');

       //要修改的新闻
       $new = $this->where(array('id' => array('eq', $id)))->find();


       if (empty($title)) {
           $this->error = '标题不能为空';
           return false;
       }
       if (mb_strlen($title) > 30) {
           $this->error = '标题长度不能超过30';
           return false;
       }

       if (empty($size)) {
           $this->error = '图片大小必填';
           return false;
       }

       $size = explode('x',$size);
       if ((!is_integer((int)$size[0])) or (!is_integer((int)$size[1]))) {
           $this->error = '图片大小输入的格式错误';
           return false;
       }
       if (empty($image)) {
           $this->error = '必须上传图片';
           return false;
       }

       $mobile_image = $new['mobile_image'];
       if ($new['image'] != $image) {
           //重新上传了图片,则需要删除原图片
           unlink(APP_PATH . $new['image']);
           unlink(APP_PATH . $new['mobile_image']);

           //更换了图片就需要压缩
           $Image = new \Think\Image();
           $Image->open(APP_PATH . $image);
           $mobile_image = '/thumb/' . date('Y-m-d', time()) . '/';
           mkdir(APP_PATH . $mobile_image, 0777, true);
           $mobile_image = $mobile_image . basename($image);
           //按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg
           $Image->thumb((int)$size[0], (int)$size[1])->save(APP_PATH . $mobile_image);
       } else {
           if ($new['size'] != I('post.size')) {
               //先删除原始的手机压缩图片
               unlink(APP_PATH . $new['mobile_image']);
               //更换了图片就需要压缩
               $Image = new \Think\Image();
               $Image->open(APP_PATH . $image);
               $mobile_image = '/thumb/' . date('Y-m-d', time()) . '/';
               mkdir(APP_PATH . $mobile_image, 0777, true);
               $mobile_image = $mobile_image . basename($image);
               //按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg
               $Image->thumb((int)$size[0], (int)$size[1])->save(APP_PATH . $mobile_image);
           }
       }

       if (is_integer($sort)) {
           $this->error = '排序必须为整数';
           return false;
       }
       if (empty($content)) {
           $this->error = '内容不能为空';
           return false;
       }

       $this->title = $title;
       $this->is_enabled = $is_enabled;
       $this->image = $image;
       $this->mobile_image = $mobile_image;
       $this->sort = $sort;
       $this->content = $content;
       $this->type = $type;
       $this->size = I('post.size');
       $this->pub_time = time();

       $this->where(array('id' => array('eq', I('post.id'))))->save();

       return true;
   }


}